package com.github.tetrisgame.ui.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.tetrisgame.utils.UIUtils;

public class JumpView extends View {
    private Path mCirleClipPath = new Path();
    private int mSize = UIUtils.dp2px(100);
    private Paint mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    private float angle = 360;
    private OnEventListener listener;

    public JumpView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mTextPaint.setTextSize(UIUtils.sp2px(20));
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(UIUtils.dp2px(6));
    }

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        this.angle = angle;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mSize = getWidth();

        mCirleClipPath.reset();
        mCirleClipPath.addCircle(getWidth() / 2, getHeight() / 2, mSize / 2, Path.Direction.CW);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        canvas.clipPath(mCirleClipPath);
        canvas.drawColor(Color.GREEN);

        float textWidth = mTextPaint.measureText("跳转");
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        canvas.drawText(
                "跳转",
                (getWidth() - textWidth) / 2f,
                getHeight() / 2f + (fontMetrics.bottom - fontMetrics.top) / 2f - fontMetrics.bottom,
                mTextPaint);

        canvas.drawArc(
                UIUtils.dp2px(3),
                UIUtils.dp2px(3),
                getWidth() - UIUtils.dp2px(3),
                getHeight() - UIUtils.dp2px(3),
                -90,
                angle,
                false,
                mArcPaint);
    }

    public void start() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(
                this,
                "angle",
                0, 360f);
        animator.setDuration(3000);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(@NonNull Animator animation) {

            }

            @Override
            public void onAnimationEnd(@NonNull Animator animation) {
                if (listener != null) {
                    listener.onAnimationEnd();
                }
            }

            @Override
            public void onAnimationCancel(@NonNull Animator animation) {

            }

            @Override
            public void onAnimationRepeat(@NonNull Animator animation) {

            }
        });
        animator.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (listener != null) {
                listener.onClicked();
            }
            return true;
        }
        return super.onTouchEvent(event);
    }

    public void setOnEventListener(OnEventListener listener) {
        this.listener = listener;
    }

    public interface OnEventListener {
        void onAnimationEnd();

        void onClicked();
    }


}







